/*
 * @Author: C.
 * @Date: 2023-01-30 09:09:50
 */
import { fieldsToMaps } from './index';
import { FormSchema } from '/@/components/Form/index';
import { useI18n } from '/@/hooks/web/useI18n';
import { getDepartmentList } from '/@/api/work/system/department';
import { stateEnum } from '/@/maps/index';
import { get_all_currency } from '/@/api/work/system/currency';
import { get_all_unit } from '/@/api/work/system/unit';
import ItemTypeApi from '/@/api/work/system/item_type';
import CustomerApi from '/@/api/work/sale/customer';
import BomApi from '/@/api/work/system/bom';
import UserApi from '/@/api/work/system/user';
import purchaseSupplierApi from '/@/api/work/purchase/supplier';
import { get_all_stockroom } from '/@/api/work/stockroom/stockroom';

const { t } = useI18n();

export type formFieldType =
  | 'DateFormat'
  | 'BillName'
  | 'BillKey'
  | 'CustomerId'
  | 'Tax'
  | 'Currency'
  | 'BillId'
  | 'BillGui'
  | 'CustomerName'
  | 'DeliveryDate'
  | 'State'
  | 'Creator'
  | 'CreationDate'
  | 'Remarks'
  | 'Id'
  | 'ItemId'
  | 'ItemName'
  | 'Description'
  | 'Unit'
  | 'Quantity'
  | 'Price'
  | 'Amount'
  | 'Stockroom'
  | 'AssociatedNo'
  | 'LotNumber'
  | 'SupplierId'
  | 'Submitter'
  | 'Department'
  | 'SupplierName'
  | 'TDA'
  | 'Keyword'
  | 'DatePeriod'
  | 'SalesReps'
  | 'ItemType'
  | 'PhotoUrl'
  | 'BomId'
  | 'RelationId'
  // 单据类型
  | 'BillType'
  // 仓库-出
  | 'StockroomOut'
  // 仓库-入
  | 'StockroomIn'
  // 库位
  | 'StockroomLocation';
export const formFields: FormSchema[] = [
  { field: 'DateFormat', label: t('commonField.DateFormat'), component: 'Select' },
  { field: 'BillName', label: t('commonField.BillName'), component: 'Input' },
  {
    field: 'BillKey',
    label: t('commonField.BillKey'),
    component: 'Input',
  },
  {
    field: 'Keyword',
    label: '关键字',
    component: 'Input',
  },
  {
    field: 'CustomerId',
    label: t('commonField.CustomerId'),
    component: 'Input',
    show: false,
  },
  { field: 'Tax', label: t('commonField.Tax'), component: 'Input' },
  {
    field: 'Currency',
    label: t('commonField.Currency'),
    component: 'ApiSelect',
    componentProps: {
      api: get_all_currency,
      resultField: 'Items',
      // use name as label
      labelField: 'Currency',
      // use id as value
      valueField: 'Currency',
      immediate: false,
    },
  },
  {
    field: 'BillId',
    label: t('commonField.BillId'),
    component: 'Input',
  },
  {
    field: 'BillGui',
    label: t('commonField.BillGui'),
    component: 'Input',
  },
  {
    field: 'CustomerName',
    label: t('commonField.CustomerName'),
    component: 'ApiSearchSelect',
    componentProps: ({ formActionType }) => {
      return {
        api: CustomerApi.api_list,
        resultField: 'Items',
        // use name as label
        labelField: 'ShortName',
        // use id as value
        valueField: 'ShortName',
        desField: 'CustomerId',
        immediate: false,
        onOptionChange: (option) => {
          formActionType.setFieldsValue({
            CustomerId: option.CustomerId,
            Tax: option.Tax,
            Currency: option.Currency,
          });
        },
      };
    },
  },
  {
    field: 'DeliveryDate',
    label: t('commonField.DeliveryDate'),
    component: 'DatePicker',
  },
  {
    field: 'State',
    label: t('commonField.State'),
    component: 'Select',
    componentProps: {
      options: stateEnum.getEnums(),
    },
  },
  {
    field: 'Creator',
    label: t('commonField.Creator'),
    component: 'Input',
  },
  {
    field: 'CreationDate',
    label: t('commonField.CreationDate'),
    component: 'Input',
  },
  {
    field: 'Remarks',
    label: t('commonField.Remarks'),
    component: 'InputTextArea',
    colProps: {
      span: 24,
      xxl: 24,
      xl: 24,
      lg: 24,
      md: 24,
      sm: 24,
      xs: 24,
    },
    componentProps: {
      allowClear: true,
    },
  },
  { field: 'Id', label: t('commonField.Id'), component: 'Input' },
  {
    field: 'ItemId',
    label: t('commonField.ItemId'),
    component: 'Input',
  },
  {
    field: 'ItemName',
    label: t('commonField.ItemName'),
    component: 'Input',
  },
  {
    field: 'Description',
    label: t('commonField.Description'),
    component: 'Input',
  },
  {
    field: 'Unit',
    label: t('commonField.Unit'),
    component: 'ApiTreeSelect',
    defaultValue: '',
    componentProps: {
      api: get_all_unit,
      resultField: 'Items',
      fieldNames: { children: 'Children', label: 'Unit', value: 'Unit' },
      immediate: false,
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'Quantity',
    label: t('commonField.Quantity'),
    component: 'Input',
  },
  {
    field: 'Price',
    label: t('commonField.Price'),
    component: 'Input',
  },
  {
    field: 'Amount',
    label: t('commonField.Amount'),
    component: 'Input',
  },
  {
    field: 'Stockroom',
    label: t('commonField.Stockroom'),
    component: 'ApiSelect',
    defaultValue: '',
    componentProps: {
      api: get_all_stockroom,
      resultField: 'Items',
      fieldNames: { children: 'Children', label: 'Stockroom', value: 'Stockroom' },
      immediate: false,
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'AssociatedNo',
    label: t('commonField.AssociatedNo'),
    component: 'Input',
  },
  // RelationId
  {
    field: 'RelationId',
    label: '相关单号',
    component: 'Input',
  },
  {
    field: 'LotNumber',
    label: t('commonField.LotNumber'),
    component: 'Input',
  },
  {
    field: 'SupplierId',
    label: t('commonField.SupplierId'),
    component: 'Input',
    show: false,
  },
  {
    field: 'Submitter',
    label: t('commonField.Submitter'),
    component: 'ApiSearchSelect',
    componentProps: ({ formActionType }) => {
      return {
        api: UserApi.api_list,
        resultField: 'Items',
        // use name as label
        labelField: 'UserName',
        // use id as value
        valueField: 'UserName',
        desField: 'Department',
        immediate: false,
        onOptionChange: (option) => {
          formActionType.setFieldsValue({
            Department: option.Department,
          });
        },
      };
    },
  },
  {
    field: 'Department',
    label: t('commonField.Department'),
    component: 'ApiTreeSelect',
    defaultValue: '',
    componentProps: {
      api: getDepartmentList,
      resultField: 'Items',
      fieldNames: { children: 'Children', label: 'Department', value: 'Department' },
      immediate: false,
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'SupplierName',
    label: t('commonField.SupplierName'),
    component: 'ApiSearchSelect',
    componentProps: ({ formActionType }) => {
      return {
        api: purchaseSupplierApi.api_list,
        resultField: 'Items',
        // use name as label
        labelField: 'ShortName',
        // use id as value
        valueField: 'ShortName',
        desField: 'SupplierId',
        immediate: false,
        onOptionChange: (option) => {
          formActionType.setFieldsValue({
            SupplierId: option.SupplierId,
            Currency: option.Currency,
          });
        },
      };
    },
  },
  { field: 'TDA', label: t('commonField.TDA'), component: 'Input' },
  {
    field: 'DatePeriod',
    label: t('commonField.DatePeriod'),
    component: 'RangePicker',
  },
  {
    field: 'SalesReps',
    label: '销售员',
    component: 'ApiSearchSelect',
    componentProps: {
      api: UserApi.api_list,
      resultField: 'Items',
      // use name as label
      labelField: 'UserName',
      // use id as value
      valueField: 'UserName',
      desField: 'Department',
      immediate: false,
      params: { Paging: false },
    },
  },
  {
    field: 'ItemType',
    label: '标签',
    component: 'ApiTreeSelect',
    componentProps: {
      api: ItemTypeApi.api_list,
      resultField: 'Items',
      fieldNames: { children: 'Children', label: 'ItemType', value: 'ItemType' },
      immediate: true,
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'PhotoUrl',
    label: '图片',
    component: 'Cropper',
    componentProps: {
      circled: false,
    },
  },
  // 物料清单 BomId
  {
    field: 'BomId',
    label: '物料清单',
    component: 'ApiSearchSelect',
    componentProps: {
      api: BomApi.api_list,
      resultField: 'Items',
      immediate: false,
      labelField: 'BomName',
      valueField: 'BillId',
      desField: 'BillId',
      params: { State: 'Approved' },
      getPopupContainer: () => document.body,
    },
  },
  // 单据类型
  {
    field: 'BillType',
    label: t('commonField.BillType'),
    component: 'Input',
  },
  // 仓库-出
  {
    field: 'StockroomOut',
    label: t('commonField.StockroomOut'),
    component: 'Input',
  },
  // 仓库-入
  {
    field: 'StockroomIn',
    label: t('commonField.StockroomIn'),
    component: 'Input',
  },
  // 库位
  {
    field: 'StockroomLocation',
    label: t('commonField.StockroomLocation'),
    component: 'Input',
  },
];

export default fieldsToMaps<FormSchema>(formFields, 'field');
